﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ForumServer;

namespace ForumServer.Forum
{
    public class Authenticator : MarshalByRefObject
    {
        hashEncode _encode;
        private List<User> _users;
        private List<Forum> _forums;
        private List<Moderator> _moderators;

        public List<User> UsersList { get { return _users; } }
        public List<Forum> ForumList { get { return _forums; } }
        public List<Moderator> ModeratorsList { get { return _moderators; } }

        public Authenticator()
        {
            _encode = new hashEncode();
            _users = new List<User>();
            _forums = new List<Forum>();
            _moderators = new List<Moderator>();
        }

        /// <summary>
        /// User object with unmasked password
        /// stored with password = sha1(password)
        /// </summary>
        /// <param name="tUser"></param>
        public void registerUser(User tUser)
        {
            tUser.Password = _encode.HashEncode(tUser.Password);
            tUser.Username = tUser.Username.ToLower();
            _users.Add(tUser);
        }

        /// <summary>
        /// User object with unmasked password
        /// stored with password = sha1(password)
        /// </summary>
        /// <param name="tUser"></param>
        public void registerModeratorUser(Moderator tUser)
        {
            tUser.Password = _encode.HashEncode(tUser.Password);
            tUser.Username = tUser.Username.ToLower();
            _moderators.Add(tUser);
        }

        /// <summary>
        /// check if username exists
        /// if exists check for password
        /// </summary>
        /// <param name="tName">user name</param>
        /// <param name="tPass">user password (encrypted)</param>
        /// <returns></returns>
        public User confirmLogin(String tName, String tPass)
        {
            tName = tName.ToLower();
            foreach (User u in _users)
            {
                if (tName.Equals(u.Username))
                {
                    if (_encode.HashEncode(tPass).Equals(u.Password))
                        return u;
                    else return null;
                }
            }

            return null;
        }

        /// <summary>
        /// check if username exists for moderators
        /// if exists check for password
        /// </summary>
        /// <param name="tName">user name</param>
        /// <param name="tPass">user password (encrypted)</param>
        /// <returns></returns>
        public Moderator confirmModeratorLogin(String tName, String tPass)
        {
            tName = tName.ToLower();
            foreach (Moderator m in _moderators)
            {
                if (tName.Equals(m.Username))
                {
                    if (_encode.HashEncode(tPass).Equals(m.Password))
                        return m;
                    else return null;
                }
            }

            return null;
        }

        /// <summary>
        /// 
        /// check UserName in use for registeration
        /// </summary>
        /// <param name="tName">name to compare</param>
        /// <returns>bool is name exists in list</returns>
        public bool checkName(String tName)
        {
            foreach (User u in _users)
                if (tName.Equals(u.Username))
                    return true;
            return false;
        }

        public List<Forum> getForums()
        {
            return _forums;
        }

    }
}
